Coverage Report

Created: 2021-08-28 18:14

D:\git\skunkworks\herald-for-cpp\herald\include\herald\analysis\logging_analysis_delegate.h
Line
Count
Source (jump to first uncovered line)
1
//  Copyright 2021 Herald Project Contributors
2
//  SPDX-License-Identifier: Apache-2.0
3
//
4
5
#ifndef HERALD_LOGGING_ANALYSIS_DELEGATE_H
6
#define HERALD_LOGGING_ANALYSIS_DELEGATE_H
7
8
#include "sampling.h"
9
#include "../data/sensor_logger.h"
10
#include "../context.h"
11
12
#include <memory>
13
#include <optional>
14
#include <functional>
15
16
namespace herald {
17
namespace analysis {
18
19
using namespace sampling;
20
21
template <typename ContextT>
22
struct OptionalSensorLogger {
23
  OptionalSensorLogger(ContextT& ctx) 
24
    : m_context(ctx)
25
      HLOGGERINIT(m_context,"herald","LoggingAnalysisDelegate")
26
3
  {
27
3
    ;
28
3
  }
29
30
  OptionalSensorLogger(const OptionalSensorLogger& other)
31
    : m_context(other.m_context)
32
      HLOGGERINIT(m_context,"herald","LoggingAnalysisDelegate")
33
0
  {
34
0
    ;
35
0
  }
36
37
  OptionalSensorLogger(OptionalSensorLogger&& other)
38
    : m_context(other.m_context)
39
      HLOGGERINIT(m_context,"herald","LoggingAnalysisDelegate")
40
  {
41
    ;
42
  }
43
44
  OptionalSensorLogger& operator=(OptionalSensorLogger&& other)
45
  {
46
    m_context = other.m_context;
47
#ifdef HERALD_LOG_LEVEL
48
#if HERALD_LOG_LEVEL != 0
49
    logger = other.logger;
50
#endif
51
#endif
52
    return *this;
53
  }
54
55
  OptionalSensorLogger& operator=(const OptionalSensorLogger& other)
56
0
  {
57
0
    m_context = other.m_context;
58
0
#ifdef HERALD_LOG_LEVEL
59
0
#if HERALD_LOG_LEVEL != 0
60
0
    logger = other.logger;
61
0
#endif
62
0
#endif
63
0
    return *this;
64
0
  }
65
66
  void debug(std::string toLog,SampledID sampled,double value)
67
  {
68
    HTDBG(toLog);
69
    // HTDBG(std::to_string(sampled));
70
    // HTDBG(std::to_string(value));
71
  }
72
73
  void debug(std::string toLog)
74
1
  {
75
1
    HTDBG(toLog);
76
1
  }
77
78
private:
79
  ContextT& m_context;
80
  HLOGGER(ContextT);
81
};
82
83
/// \brief Logs any given type of sample to the Herald logging subsystem as a Debug message
84
template <typename ContextT, typename ValT>
85
struct LoggingAnalysisDelegate {
86
  using value_type = ValT;
87
  
88
  LoggingAnalysisDelegate()
89
    : ctx(),
90
      logger()
91
  {
92
    ;
93
  }
94
95
  LoggingAnalysisDelegate(ContextT& context)
96
    : ctx(context),
97
      logger(ctx)
98
1
  {
99
1
    ;
100
1
  }
101
102
  LoggingAnalysisDelegate(const LoggingAnalysisDelegate& other) noexcept
103
    : ctx(other.ctx),
104
      logger(ctx)
105
  {
106
    ;
107
  }
108
109
  LoggingAnalysisDelegate(LoggingAnalysisDelegate&& other) noexcept
110
    : ctx(other.ctx),
111
      logger(ctx)
112
2
  {
113
2
    ;
114
2
  }
115
  
116
3
  ~LoggingAnalysisDelegate() = default;
117
118
0
  LoggingAnalysisDelegate& operator=(LoggingAnalysisDelegate&& other) noexcept {
119
0
    ctx = other.ctx;
120
0
    logger = other.logger;
121
0
    return *this;
122
0
  }
123
124
  void assignContext(ContextT& newContext) noexcept {
125
    ctx = newContext;
126
    logger.emplace(ctx);
127
  }
128
129
  // specific override of template
130
1
  void newSample(SampledID sampled, Sample<ValT> sample) {
131
1
    // Log the read distance as debug
132
1
    if (!logger.has_value()) { // GUARD
133
0
      return;
134
0
    }
135
1
    logger.value().debug("New Sample Recorded.");
136
1
    // logger.value().debug("New Sample Recorded. SampledID: {}, Value: {}",sampled,(double)sample);
137
1
  }
138
139
private:
140
  std::optional<std::reference_wrapper<ContextT>> ctx;
141
  std::optional<OptionalSensorLogger<ContextT>> logger;
142
};
143
144
}
145
}
146
147
#endif